package com.zxy.ziems.server.listener;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zxy.btp.common.exception.ConditionException;
import com.zxy.btp.common.util.IdWorker;
import com.zxy.btp.common.util.SpringContextUtils;
import com.zxy.ziems.server.constant.PointDataType;
import com.zxy.ziems.server.classify.pojo.entity.Classify;
import com.zxy.ziems.server.domain.PointData;
import com.zxy.ziems.server.dto.PointDataExportDto;
import com.zxy.ziems.server.mapper.PointDataMapper;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName PointDataListener
 * @Description
 * @Author 邓伟
 * @Date 2023/12/4 16:31
 * @Version 1.0
 */
@Slf4j
public class PointDataListener extends AnalysisEventListener<PointDataExportDto> {

    private PointDataMapper pointDataMapper;
    private String userId;
    private String userName;

    private Classify classify;

    private List<PointData> list = new ArrayList<>();

    private Set<String> pointCodeSet;

    private Map<LocalDateTime,Set<String>> dataMap = new HashMap();

    private IdWorker idWorker;

    public PointDataListener(PointDataMapper pointDataMapper, String userId, String userName, Classify classify, Set<String> pointCodeSet) {
        this.pointDataMapper = pointDataMapper;
        this.userId = userId;
        this.userName = userName;
        this.classify = classify;
        this.pointCodeSet = pointCodeSet;
        idWorker = SpringContextUtils.getBean(IdWorker.class);
    }

    @Override
    public void invoke(PointDataExportDto pointDataExportDto, AnalysisContext analysisContext) {
        LocalDateTime dataTime = null;
        Integer dataObtainType = classify.getDataObtainType();
        String dataTimeStr = pointDataExportDto.getDataTime();
        if (dataObtainType == PointDataType.HAND_DAY) {
            dataTime = LocalDateTime.parse(dataTimeStr+" 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }else if (dataObtainType == PointDataType.HAND_MONTH){
            dataTime = LocalDateTime.parse(dataTimeStr + "-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }else {
            throw new ConditionException("数据时间格式有误，请参考示例填写");
        }
        //进行数据校验
        //判断此点位是否关联此分类
        String pointCode = pointDataExportDto.getPointCode();
        if (!pointCodeSet.contains(pointCode)) {
            throw new ConditionException("点位"+ pointCode +"未关联分类，请关联后再导入");
        }
        //校验点位数据是否重复
        Set<String> codeSet = dataMap.get(dataTime);
        if (ObjectUtil.isNotEmpty(codeSet)) {
            if (codeSet.contains(pointCode)) {
                throw new ConditionException("点位编码为："+ pointCode +"，数据时间为：" + dataTimeStr + "，数据重复，请修改后再导入");
            }else {
                codeSet.add(pointCode);
            }
        }else {
            codeSet = new HashSet<>();
            codeSet.add(pointCode);
            dataMap.put(dataTime,codeSet);
        }

        PointData pointData = new PointData();
        pointData.setDataValue(pointDataExportDto.getDataValue());
        pointData.setUpdateTime(LocalDateTime.now());
        pointData.setUpdateUserName(userName);
        pointData.setUpdateUserId(userId);
        pointData.setPointCode(pointCode);
        pointData.setDataObtainType(dataObtainType);
        pointData.setDataTime(dataTime);
        pointData.setId(idWorker.nextId() + "");
        list.add(pointData);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (CollectionUtil.isNotEmpty(list)) {
            pointDataMapper.batchInsert(list);
        }
    }
}
